package com.ruoyi.pos.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.pos.domain.system.PosSystemConfig;
import com.ruoyi.pos.domain.dto.system.request.SystemConfigQueryRequest;
import com.ruoyi.pos.domain.dto.system.request.SystemConfigUpdateRequest;
import com.ruoyi.pos.domain.dto.system.response.SystemConfigResponse;
import com.ruoyi.pos.domain.dto.common.response.BasePageResponse;
import com.ruoyi.pos.mapper.system.PosSystemConfigMapper;
import com.ruoyi.pos.service.system.IPosSystemConfigService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;

/**
 * POS系统配置Controller
 * 
 * @author ruoyi
 * @date 2025-01-30
 */
@Api(tags = "POS系统配置管理")
@RestController
@RequestMapping("/pos/config")
public class PosSystemConfigController extends BaseController {

    @Autowired
    private IPosSystemConfigService posSystemConfigService;
    
    @Autowired
    private PosSystemConfigMapper posSystemConfigMapper;

    /**
     * 查询系统配置列表
     */
    @ApiOperation("查询系统配置列表")
    @PreAuthorize("@ss.hasPermi('pos:config:list')")
    @GetMapping("/list")
    public TableDataInfo list(SystemConfigQueryRequest request) {
        startPage();
        BasePageResponse<SystemConfigResponse> response = posSystemConfigService.querySystemConfigs(request);
        TableDataInfo dataTable = new TableDataInfo();
        dataTable.setCode(response.getCode());
        dataTable.setMsg(response.getMsg());
        dataTable.setRows(response.getRows());
        dataTable.setTotal(response.getTotal());
        return dataTable;
    }

    /**
     * 导出系统配置列表
     */
    @ApiOperation("导出系统配置列表")
    @PreAuthorize("@ss.hasPermi('pos:config:export')")
    @Log(title = "POS系统配置", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PosSystemConfig posSystemConfig) {
        List<PosSystemConfig> list = posSystemConfigMapper.selectPosSystemConfigList(posSystemConfig);
        ExcelUtil<PosSystemConfig> util = new ExcelUtil<PosSystemConfig>(PosSystemConfig.class);
        util.exportExcel(response, list, "系统配置数据");
    }

    /**
     * 获取系统配置详细信息
     */
    @ApiOperation("获取系统配置详细信息")
    @PreAuthorize("@ss.hasPermi('pos:config:query')")
    @GetMapping(value = "/{configId}")
    public AjaxResult getInfo(@ApiParam("配置ID") @PathVariable Long configId) {
        SystemConfigQueryRequest request = new SystemConfigQueryRequest();
        request.setConfigId(configId);
        return success(posSystemConfigService.getSystemConfig(request));
    }

    /**
     * 根据配置键获取配置值
     */
    @ApiOperation("根据配置键获取配置值")
    @GetMapping(value = "/key/{configKey}")
    public AjaxResult getConfigByKey(@ApiParam("配置键") @PathVariable String configKey) {
        String configValue = posSystemConfigService.getConfigValue(configKey);
        return success(configValue);
    }

    /**
     * 新增系统配置
     */
    @ApiOperation("新增系统配置")
    @PreAuthorize("@ss.hasPermi('pos:config:add')")
    @Log(title = "POS系统配置", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody PosSystemConfig posSystemConfig) {
        return toAjax(posSystemConfigMapper.insertPosSystemConfig(posSystemConfig));
    }

    /**
     * 修改系统配置
     */
    @ApiOperation("修改系统配置")
    @PreAuthorize("@ss.hasPermi('pos:config:edit')")
    @Log(title = "POS系统配置", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody PosSystemConfig posSystemConfig) {
        return toAjax(posSystemConfigMapper.updatePosSystemConfig(posSystemConfig));
    }

    /**
     * 批量更新系统配置
     */
    @ApiOperation("批量更新系统配置")
    @PreAuthorize("@ss.hasPermi('pos:config:edit')")
    @Log(title = "批量更新系统配置", businessType = BusinessType.UPDATE)
    @PutMapping("/batch")
    public AjaxResult batchUpdate(@Valid @RequestBody SystemConfigUpdateRequest request) {
        boolean result = posSystemConfigService.updateSystemConfigs(request);
        return toAjax(result ? 1 : 0);
    }

    /**
     * 删除系统配置
     */
    @ApiOperation("删除系统配置")
    @PreAuthorize("@ss.hasPermi('pos:config:remove')")
    @Log(title = "POS系统配置", businessType = BusinessType.DELETE)
    @DeleteMapping("/{configIds}")
    public AjaxResult remove(@ApiParam("配置ID数组") @PathVariable Long[] configIds) {
        return toAjax(posSystemConfigMapper.deletePosSystemConfigByIds(configIds));
    }

    /**
     * 刷新配置缓存
     */
    @ApiOperation("刷新配置缓存")
    @PreAuthorize("@ss.hasPermi('pos:config:refresh')")
    @Log(title = "刷新配置缓存", businessType = BusinessType.OTHER)
    @DeleteMapping("/refreshCache")
    public AjaxResult refreshCache() {
        // 目前配置没有使用缓存，直接返回成功
        return success("配置缓存刷新成功");
    }

    /**
     * 根据配置分组获取配置列表
     */
    @ApiOperation("根据配置分组获取配置列表")
    @PreAuthorize("@ss.hasPermi('pos:config:query')")
    @GetMapping("/group/{configGroup}")
    public AjaxResult getByGroup(@ApiParam("配置分组") @PathVariable String configGroup) {
        PosSystemConfig query = new PosSystemConfig();
        query.setCategory(configGroup);  // 使用category字段而不是configGroup
        List<PosSystemConfig> list = posSystemConfigMapper.selectPosSystemConfigList(query);
        return success(list);
    }
}

